﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using MasApp.BLL;


public partial class _Default : System.Web.UI.Page
{
    public string has_login_page = "~/public/roleman.aspx";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (LoginBasePage.hasLogin(Page.Session))
        {
            Page.Server.Transfer(this.has_login_page);
        }
        tb_username.Focus();
    }
    protected void login(object sender, EventArgs e)
    {
        string validatekey = SessUtil.getValidateKey(this.Session).ToLower();

        string vcode = Request[SessUtil.validate_code_name] == null ? string.Empty : Request[SessUtil.validate_code_name].ToString().ToLower();

        if (vcode != validatekey)
        {
            ResponseHelper.alert("验证码错误！", "default.aspx");
        }
        else
        {
            int[] permission = new int[28];
            string username = tb_username.Text;
            string password = tb_password.Text;
            int userid = MasApp.DbUtil.Db.login(username, password);
            if (userid != 0)
            {
                Session["userid"] = userid;
                Session["username"] = username;
                Session["organizationid"] = MasApp.BLL.Search.SearchOrgID(username);
                SqlDataReader dr = MasApp.DbUtil.Db.login(userid);
                while (dr.Read())
                {
                    string drstr = dr["permission"].ToString();
                    for (int i = 0; i < drstr.Length; i++)
                    {
                        if (drstr.Substring(i, 1) == "1")
                        {
                            permission[i] = 1;
                        }
                    }
                }
                dr.Close();
                if (permission != null)
                {
                    Response.Write("您的权限为：");
                    for (int i = 0; i < permission.Length; i++)
                    {
                        Response.Write(permission[i].ToString());
                    }
                }

                Response.Redirect("~/public/roleman.aspx");
            }
            else
            {
                Response.Write("<script language='javascript' type='text/javascript'>"
                    + "window.alert('用户名错误或密码错误！');location.href='default.aspx';</script>");
            }
        }
    }
}
